<%--
  Created by IntelliJ IDEA.
  User: leixiaolong
  Date: 2018/11/22
  Time: 12:06 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>机构编辑管理</title>
    <link rel="stylesheet" href="/static/css/rpt-condition.css" media="all" type="text/css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/layui/css/layui.css" media="all"
          type="text/css">

    <style>
        .layui-input-inline {
            min-width: 300px;
            max-width: 600px;
        }
    </style>
</head>
<body>
<div>
    <div class="layui-row layui-col-space10" style="margin: 10px; background-color: white">
        <div class="layui-container">
            <div class="layui-form" lay-filter="select-condition">
                <div class="layui-form-item layui-col-md12 layui-hide">
                    <label class="layui-form-label ">机构id：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="organ_id" name="id" class="layui-input-inline dom-add-input">
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label ">机构名称：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="organ_name" name="organName" class="layui-input-inline dom-add-input"
                               lay-verify="required">
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label ">机构等级：</label>
                    <div class="layui-input-inline">
                        <select id="organ_level" name="organLevel" class="layui-form-select" lay-verify="required">
                            <option value="" selected>请选择</option>
                            <option value=1>一级机构</option>
                            <option value=2>二级机构</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label">上级机构：</label>
                    <div class="layui-input-inline">
                        <select id="father_organ" name="fatherOrgan" class="layui-form-select">
                            <option value="" selected>请选择</option>
                            <option value=1>上级机构1</option>
                            <option value=2>机构2</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label">状态：</label>
                    <div class="layui-input-inline">
                        <input type="checkbox" id="organ_stat" name="stat" lay-skin="switch" lay-text="启用|停用">
                    </div>
                </div>
                <div class="layui-btn-group">
                    <button class="layui-btn layui-btn-normal layui-btn-warm" lay-submit data-type="confirm">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script language='javascript' src="${pageContext.request.contextPath}/plugins/layui/layui.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-select.js"></script>

<script>
    layui.use(["form", "jquery"], function () {
        var form = layui.form, $ = layui.jquery;

        initOrganSelect('father_organ', '/sys/getOrganList', null);

        var actives = {
            confirm: function (data) {
                debugger
                var field = data;
                //未选中checkbos时，value无默认值，手工赋值为1-停用
                if (field.stat == null) {
                    field.stat = 1;
                }

                $.ajax({
                    url: "/sys/editOrganInfo",
                    contentType: 'GET',
                    sync: true,
                    data: field,
                    success: function (data) {
                        layer.msg("交易成功," + JSON.stringify(data));
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);

                        //刷新父页面table
                        // window.parent.reloadTable();

                        parent.layui.table.reload("tab_organ", {});
                    },
                    error: function (data) {
                        layer.msg("交易失败, message:" + data.rspMsg)
                    }
                });
            },
            cancel: function () {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            }
        }

        form.on('submit', function (data) {
            debugger
            var elem = data.elem;
            var field = data.field;
            actives[$(this).data("type")].call(this, field);
        });

        form.on('switch', function (data) {
            var switchId = data.elem.id;

            if (true == data.elem.checked) {
                $('#' + switchId).val(0);
            } else {
                $('#' + switchId).val(1);
            }
        });

    });

    function init(data) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            debugger
            $('#organ_id').val(data.organCode);
            $('#organ_name').val(data.organName);
            $('#organ_level').val(data.organLevel == 0 ? "" : data.organLevel);
            $('#father_organ').val(data.fatherOrganCode == 0 ? "" : data.fatherOrganCode);
            if (data.stat == 0) {
                $('#organ_stat').attr("checked", true);
            } else {
                $('#organ_stat').remove("checked");
            }
            $('#organ_stat').val(data.stat);

            form.render('checkbox');
            form.render('select');
        });
    }
</script>
